<template>
  <div class="app-container org">
    <el-card>
      <el-row>
        <el-col :span="24">
          <p class="pinformation">搜索条件</p>
        </el-col>
      </el-row>
      <el-form ref="queryRef" label-width="100" :model="queryParams">
        <el-row>
          <el-col :span="3">
            <el-form-item label="入库批次编号：" prop="wxfwCode">
              <el-input clearable maxlength="100" placeholder="请输入入库批次编号" v-model="queryParams.wxfwCode"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="容器/包装编码：" prop="containerCode" label-width="115">
              <el-input clearable maxlength="100" placeholder="请输入" v-model="queryParams.containerCode"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="危险品俗称/名录名称：" prop="wxfwName" label-width="150">
              <el-input clearable maxlength="100" placeholder="请输入" v-model="queryParams.wxfwName"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="危废类别：" prop="wxfwType" label-width="80">
              <el-select placeholder="请选择" clearable v-model="queryParams.wxfwType" filterable>
                <el-option v-for="dict in proxy.addListNoLimit(wxfw_type)" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-form-item label="状态：" prop="status" label-width="50">
              <el-select placeholder="请选择" clearable v-model="queryParams.status" filterable>
                <el-option v-for="dict in proxy.addListNoLimit(wxfw_status)" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="入库日期：" prop="createDate" label-width="80">
              <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" :clearable="false"
                :editable="false" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-form-item label="入库人：" prop="wxfwUser" label-width="60">
              <el-input clearable maxlength="100" placeholder="请输入" v-model="queryParams.wxfwUser"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-row justify="center">
              <el-button type="success" @click="handleQuery">搜索</el-button>
              <el-button class="resetBtn" @click="resetQuery">重置</el-button>
            </el-row>
          </el-col>
        </el-row>
      </el-form>
      <el-row>
        <p class="pinformation">危险废物入库列表</p>
        <p class="totalStyle">共 {{ total }} 条数据</p>
        <el-button type="success" link icon="Plus" @click="singleAdd"
          style="padding-bottom: 8px; margin-left: 10px">新增入库</el-button>
        <el-row style="padding-bottom: 5px">
          <el-button type="success" style="margin-left: 30px" link icon="Plus">贮存批量签字</el-button>
          <el-button type="success" style="margin-left: 10px" link icon="Plus" @click="importTemplate">导出数据</el-button>
          <el-button type="success" style="margin-left: 10px" link icon="Plus">批量入库</el-button>
          <el-button type="success" style="margin-left: 10px" link icon="Plus" @click="handleExport">导出入库记录</el-button>
        </el-row>
        <el-table ref="mainTableRef" :data="wxfwTable" v-loading="loading">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column show-overflow-tooltip label="入库批次编号" prop="wxfwCode" align="center" width="110" />
          <el-table-column show-overflow-tooltip label="入库时间" prop="createDate" align="center" width="100" />
          <el-table-column show-overflow-tooltip label="容器/包装编码" prop="containerCode" align="center" width="120" />
          <el-table-column show-overflow-tooltip label="容器/包装类型" prop="containerTypeName" align="center" width="120" />
          <el-table-column show-overflow-tooltip label="容器/包装数量" prop="containerCount" align="center" width="120" />
          <el-table-column show-overflow-tooltip label="内部名称" prop="internalName" align="center" />
          <el-table-column show-overflow-tooltip label="国家名录名称" prop="coudirName" align="center" />
          <el-table-column show-overflow-tooltip label="危废类别" prop="wxfwTypeName" align="center" width="80" />
          <el-table-column show-overflow-tooltip label="危废代码" prop="wxfwNumber" align="center" width="80" />
          <el-table-column show-overflow-tooltip label="入库量" prop="createCount" align="center" width="80" />
          <el-table-column show-overflow-tooltip label="计量单位" prop="unitName" align="center">
            <template #default="scope">
              <span v-html="transformation(scope.row.unitName)"></span>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip label="运送部门经办人" prop="transportUser" align="center" />
          <el-table-column show-overflow-tooltip label="贮存部门经办人" prop="storageUser" align="center" />
          <el-table-column show-overflow-tooltip label="产生批次编码" prop="batchCode" align="center" />
          <el-table-column show-overflow-tooltip label="状态" prop="status" align="center" width="60">
            <template #default="scope">
              <dict-tag :options="wxfw_status" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
            <template #default="scope">
              <el-button link type="success" @click="xqBzwz(scope.row.wxfwId, 1)">复制</el-button>
              <!-- <el-button
                link
                type="success"
                @click="xqBzwz(scope.row.wxfwId, 3)"
                >编辑</el-button
              > -->
              <el-button link type="success">签字</el-button>
              <el-button link type="success" @click="delBzwz(scope.row.wxfwId)" v-if="!scope.row.useGg">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize" @pagination="getList" />
      </el-row>
    </el-card>
    <!-- 单个入库和引用弹窗 -->
    <el-dialog :class="'el-dialog-theme3'" append-to-body draggable :close-on-click-modal="false"
      @close="closedInDialog" v-model="wfOutOpen" :title="wfOutTitle" width="1600">
      <el-form label-width="80" ref="rkOrYyRef" :model="form" :rules="rules">
        <el-row>
          <el-col :span="8">
            <el-form-item label="入库批次编号：" prop="wxfwCode" label-width="110">
              <el-input clearable maxlength="100" v-model="form.wxfwCode"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="入库日期：" prop="createDate">
              <el-date-picker v-model="form.createDate" placeholder="请选择要求完成时间" type="date" style="width: 100%"
                value-format="YYYY-MM-DD" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="运送部门经办人：" label-width="120" v-model="form.transportUser">
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-row justify="end">
              <el-button type="success">签字</el-button>
            </el-row>
          </el-col>
        </el-row>
        <el-row>
          <p class="pinformation">入库列表</p>
          <p class="totalStyle">共 {{ form.wxfwList.length }} 条数据</p>
          <el-row style="padding-bottom: 5px">
            <el-button link icon="Plus" @click="addWxfwList" style="margin-left: 10px">新增</el-button>
          </el-row>
        </el-row>
        <el-table :data="form.wxfwList">
          <el-table-column show-overflow-tooltip label="容器/包装编码" prop="containerCode" align="center" />
          <el-table-column show-overflow-tooltip label="容器/包装类型" prop="containerType" align="center">
            <template #default="scope">
              <dict-tag :options="container_type" :value="scope.row.containerType" />
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip label="容器/包装数量" prop="containerCount" align="center" />
          <el-table-column show-overflow-tooltip label="内部名称" prop="internalName" align="center" />
          <el-table-column show-overflow-tooltip label="国家名录名称" prop="coudirName" align="center" />
          <el-table-column show-overflow-tooltip label="危废类别" prop="wxfwType" align="center">
            <template #default="scope">
              <dict-tag :options="wxfw_type" :value="scope.row.wxfwType" />
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip label="危废代码" prop="wxfwNumber" align="center" />
          <el-table-column show-overflow-tooltip label="入库量" prop="createCount" align="center" />
          <el-table-column show-overflow-tooltip label="计量单位" prop="unitName" align="center">
            <template #default="scope">
              <span v-html="transformation(scope.row.unitName)"></span>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip label="产生批次编码" prop="batchCode" align="center" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template #default="scope">
              <el-button link type="success" @click="copyJcxm(scope.row)">复制</el-button>
              <el-button link type="success" @click="delJcxm(scope.$index)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="success" @click="saveWxfw">保 存</el-button>
          <el-button type="danger" @click="wfOutOpen = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog :class="'el-dialog-theme3'" append-to-body draggable :close-on-click-modal="false"
      v-model="open" :title="title" @close="closedDialog" width="1000">
      <el-form label-width="120" :model="inForm" :rules="inFormRules" ref="formRef">
        <el-row>
          <el-col :span="8">
            <el-form-item label="容器/包装编码：" prop="containerCode">
              <el-input placeholder v-model="inForm.containerCode" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="容器/包装类型：" prop="containerType">
              <el-select placeholder="请选择" clearable v-model="inForm.containerType" filterable>
                <el-option v-for="dict in container_type" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="容器/包装数量：" prop="containerCount">
              <el-input-number placeholder="请输入容器/包装数量" :min="1" style="width: 100%" controls-position="right"
                v-model="inForm.containerCount" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="内部名称：" prop="internalName">
          <el-input placeholder v-model="inForm.internalName" clearable />
        </el-form-item>
        <el-row>
          <el-col :span="8">
            <el-form-item label="国家名录名称：" prop="coudirName">
              <el-input placeholder v-model="inForm.coudirName" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="危废类别：" prop="wxfwType">
              <el-select placeholder="请选择" clearable v-model="inForm.wxfwType" filterable>
                <el-option v-for="dict in wxfw_type" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="危废代码：" prop="wxfwNumber">
              <el-input placeholder v-model="inForm.wxfwNumber" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="入库量：" prop="createCount">
              <el-input-number placeholder="请输入入库量" :min="1" style="width: 100%" controls-position="right"
                v-model="inForm.createCount" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="计量单位：" prop="unitId">
              <el-select v-model="inForm.unitId" clearable filterable style="width: 100%">
                <el-option v-for="item in unitList" :key="item.unitId" :label="item.unitName" :value="item.unitId">
                  <span v-html="transformation(item.unitName)"></span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="产生批次编码：" prop="batchCode">
              <el-input placeholder v-model="inForm.batchCode" clearable />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="success" @click="inSave">确 定</el-button>
          <el-button type="danger" @click="open = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="WxfwOutin">
import onlinePreview from "@/components/onlinePreview/index.vue";
import {
  listLedger,
  delWxfwList,
  wxfwViewForm,
  addWxfw,
} from "@/api/resource/wxfw";
import { getCurrentTime } from "@/api/common";
import { getUnitList } from "@/api/store/unit";
import { listUser } from "@/api/contract/check";

const { proxy } = getCurrentInstance();
const { wxfw_status, wxfw_type, container_type } = proxy.useDict(
  "wxfw_status",
  "wxfw_type",
  "container_type"
);

const wxfwTable = ref([]);
const wfOutOpen = ref(false);
const loading = ref(false);
const wfOutTitle = ref("");
const open = ref(false);
const title = ref("");
const serialTypeId = ref(37);
const onlinePreviewRef = ref(null);
function emitForm(val) {
  form.value.params["serialData"] = val;
}
const data = reactive({
  form: { wxfwList: [] },
  inForm: {},
  queryParams: {
    pageNum: 1, // 当前页数
    pageSize: 10, // 当前页数据条数
    wxfwType: -1,
    status: -1,
  },
  rules: {
    wxfwCode: [
      { required: true, message: "入库批次编号不能为空", trigger: "blur" },
    ],
  },
  inFormRules: {
    containerCode: [
      { required: true, message: "容器/包装编码不能为空", trigger: "blur" },
    ],
    containerType: [
      { required: true, message: "容器/包装类型不能为空", trigger: "blur" },
    ],
    containerCount: [
      { required: true, message: "容器/包装数量不能为空", trigger: "blur" },
    ],
    internalName: [
      { required: true, message: "内部名称不能为空", trigger: "blur" },
    ],
    coudirName: [
      { required: true, message: "国家名录名称不能为空", trigger: "blur" },
    ],
    wxfwType: [
      { required: true, message: "危废类别不能为空", trigger: "blur" },
    ],
    wxfwNumber: [
      { required: true, message: "危废代码不能为空", trigger: "blur" },
    ],
    createCount: [
      { required: true, message: "入库量不能为空", trigger: "blur" },
    ],
    unitId: [{ required: true, message: "计量单位不能为空", trigger: "blur" }],
    batchCode: [
      { required: true, message: "产生批次编号不能为空", trigger: "blur" },
    ],
  },
});

const { queryParams, form, rules, inForm, inFormRules } = toRefs(data);

const heightParams = ref([{ tableName: "main", bottomHeight: 21 }]);

function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}
onMounted(() => {
  nextTick(() => {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  });
});
onUpdated(() => {
  nextTick(function () {
    getTableHeight();
  });
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});

const total = ref(0);
const dateRange = ref([]);
function getList() {
  loading.value = true;
  listLedger(proxy.addDateRange(queryParams.value, dateRange.value)).then(
    (res) => {
      wxfwTable.value = res.rows;
      loading.value = false;
      total.value = res.total;
    }
  );
}
getList();

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

function closedDialog() {
  inForm.value = {};
  proxy.$refs["formRef"]?.clearValidate();
}

function delBzwz(wxfwId) {
  proxy.$modal
    .confirm("是否确认删除该条记录？")
    .then(function () {
      delWxfwList(wxfwId).then((res) => {
        proxy.$modal.msgSuccess("删除成功");
        getList();
      });
    })
    .catch(() => { });
}

function xqBzwz(bzwzId, isOpen) {
  wxfwViewForm(bzwzId).then((res) => {
    form.value = res.data;
    form.value.params = {};

    if (isOpen == 1) {
      wfOutOpen.value = true;
      wfOutTitle.value = "复制危险废物";
      delete form.value.bzwzId;
    } else if (isOpen == 3) {
      wfOutOpen.value = true;
      wfOutTitle.value = "编辑危险废物";
    }
  });
}

function closedInDialog() {
  form.value = {
    wxfwList: [],
    fileList: [],
    params: {},
  };
  proxy.$refs["rkOrYyRef"]?.clearValidate();
}

function singleAdd() {
  getCurrentTime().then((res) => {
    form.value.createDate = res.data;
  });
  wfOutOpen.value = true;
  wfOutTitle.value = "危险废物入库";
  form.value.params = {};
}

function saveWxfw() {
  proxy.$refs["rkOrYyRef"].validate((valid) => {
    if (valid) {
      if (form.value.wxfwList.length) {
        addWxfw(form.value).then((res) => {
          wfOutOpen.value = false;
          proxy.$modal.msgSuccess(res.msg);
          getList();
        });
      } else {
        proxy.$modal.msgError("入库列表不能为空");
      }
    }
  });
}

function delJcxm(row) {
  proxy.$modal
    .confirm("是否确认该条删除数据项？")
    .then(function () {
      form.value.wxfwList.splice(row, 1);
    })
    .then(() => {
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => { });
}

function copyJcxm(row) {
  inForm.value = JSON.parse(JSON.stringify(row));
  delete inForm.value.wxfwId;
  open.value = true;
  title.value = "复制危险废物";
}

const unitList = ref([]);
getUnitList({}).then((res) => {
  unitList.value = res.data;
});

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [{ tableName: "main", bottomHeight: 73 }];
    } else {
      heightParams.value = [{ tableName: "main", bottomHeight: 21 }];
    }
    getTableHeight();
  }
);

function transformation(row) {
  return proxy.getSupAndSub(row);
}

const userList = ref([]);
listUser().then((res) => {
  userList.value = res.data;
});

// serialTypeId.value = 38

/** 导出按钮操作 */
function handleExport() {
  return;
  proxy.download(
    "/resource/bzwz/export",
    {
      ...queryParams.value,
    },
    `危险废物入库登记表_${new Date().getTime()}.xlsx`
  );
}

/** 下载模板操作 */
function importTemplate() {
  return;
  proxy.download(
    "/resource/bzwz/importTemplate",
    {},
    `危险废物模板_${new Date().getTime()}.xlsx`
  );
}

function addWxfwList() {
  title.value = "新增危险废物";
  open.value = true;
}

function inSave() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      inForm.value.unitName = proxy.getItemByArray(
        unitList.value,
        "unitId",
        inForm.value.unitId,
        "unitName"
      );
      inForm.value.wxfwId = "1234567890";
      form.value.wxfwList.unshift(inForm.value);
      open.value = false;
    }
  });
}
</script>

<style lang="scss" scoped>
.el-radio {
  margin-right: 5px;
}
</style>
